import 'package:get/get.dart';
import 'package:tiktokadult/tools_base/ad_insert_manager.dart';
import 'package:tiktokadult/tools_base/global_store/local_ads_info_store.dart';
import '../../../hj_model/home/home_tabs_entity.dart';
import '../../../hj_model/home/module_detail_model.dart';
import '../../../hj_utils/api_service/vid_service.dart';
import '../../../tools_base/debug_log.dart';
import 'home_tab_base_logic.dart';

class HomeTabSectionLogic extends HomeTabBaseLogic {
  HomeTabSectionLogic(int index, ModuleData tabModel, {bool isDarkStyle = false}) : super(index, tabModel, isDarkStyle: isDarkStyle);

  bool lastIsGuessLike = false; // true 最后一个是猜你喜欢
  int guessLikeSortType = 0; // 0 最多观看 1 最新上架 3 最多收藏
  List<int> gLikeSortParam = [0, 1, 3];
  int guessLikePage = 1;

  @override
  void onReady() {
    initData();
    super.onReady();
  }

  void sortGuessLikeExchange(int value) {
    if (value == guessLikeSortType) {
      return;
    }
    if (lastIsGuessLike) {
      allSection.last.allVideoInfo = null;
    }
    guessLikeSortType = value;
    update();
    _loadGuessLike(page: 1, sortType: guessLikeSortType);
  }

  void initData() async {
    isLoadingData = true;
    update();
    _loadData();
  }

  void refreshData() {
    _loadData();
  }

  void loadMoreData() {
    if (lastIsGuessLike) {
      _loadGuessLike(page: guessLikePage + 1, sortType: guessLikeSortType);
    } else {
      _loadData(page: currentPage + 1);
    }
  }

  void _loadData({int page = 1, int size = 5}) async {
    try {
      ModuleDetailModel? retResp = await VidService.getModuleDetail(tabModel.id ?? '', pageNumber: page, pageSize: size);

      currentPage = page;
      retResp?.allSection?.removeWhere((element) => element.allVideoInfo?.isEmpty ?? true);
      if (currentPage == 1) {
        dataSource = retResp;
      } else {
        dataSource?.allSection?.addAll(retResp?.allSection ?? []);
      }
      AdInsertManager.section(
        dataSource?.allSection ?? [],
        getAdvByTypeSync(5),
        adGap: 2,
      );
      if (retResp?.hasNext == true) {
        lastIsGuessLike = false;
        refreshController?.loadComplete();
      } else {
        if (retResp?.allSection?.last.isGuessLike == true) {
          lastIsGuessLike = true;
          guessLikeSortType = 0;
          guessLikePage = 1;
          if (dataSource?.allSection?.last.isAdsArr() == true) {
            dataSource?.allSection?.removeLast();
          }
          retResp?.allSection?.last.allVideoInfo = null;
          update();
          await _loadGuessLike();
        } else {
          lastIsGuessLike = false;
          refreshController?.loadNoData();
        }
      }
    } catch (e) {
      debugLog(e);
      refreshController?.loadComplete();
    }
    refreshController?.refreshCompleted();
    isLoadingData = false;
    update();
  }

  Future _loadGuessLike({int page = 1, int size = 10, int sortType = 0}) async {
    try {
      AllSection lastSection = allSection.last;
      int sortValue = gLikeSortParam[sortType];
      AllSection? retModel = await VidService.getGuessLike(page, size, tabModel.id ?? "", sortValue);
      if (sortType != guessLikeSortType) {
        return;
      }
      guessLikePage = page;
      lastSection.allVideoInfo ??= [];
      if (lastSection.allVideoInfo?.isNotEmpty == true && page == 1) {
        lastSection.allVideoInfo?.clear();
      }
      lastSection.allVideoInfo?.addAll(retModel?.allVideoInfo ?? []);
      if (retModel?.hasNext == false) {
        refreshController?.loadNoData();
      } else {
        refreshController?.loadComplete();
      }
    } catch (e) {
      refreshController?.loadComplete();
    }
    refreshController?.refreshCompleted();
    update();
  }
}
